﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoccerSimulator5000
{
    public partial class FinanceForm : Form
    {
        List<Sponsor> sponsorList = new List<Sponsor>();
        public FinanceForm()
        {
            InitializeComponent();

            GenerateSponsors();

            if (Game.player.playerTeam.finance.sponsor.duration == 0)
            {
                this.sponsorListBox.Items.Clear();
                foreach (var Sponsor in sponsorList)
                {
                    this.sponsorListBox.Items.Add(Sponsor);
                }
                this.sponsorListBox.DisplayMember = "displayMember";
            }
            
            transactionsDataGridView.DataSource = Game.player.playerTeam.finance.transactions;
        }

        public void GenerateSponsors()
        {
            for (int i = 0; i < 5; i++)
            {
                sponsorList.Add(new Sponsor(1, 15000*(i+1), 1000000 - (100000*(i+1))));
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            if (Game.player.playerTeam.finance.stadium.level == Stadium.Level.five)
            {
                this.upgradeStadiumButton.Enabled = false;
            }

            
            e.Graphics.DrawString("Total bankbalance: " + Game.player.playerTeam.finance.bankTotal.ToString(), new Font(System.Drawing.FontFamily.GenericSansSerif, 12), Brushes.Black, 100, 540);

        }

        public void UpdateDataGridView()
        {
            transactionsDataGridView.DataSource = null;
            transactionsDataGridView.DataSource = Game.player.playerTeam.finance.transactions;
            transactionsDataGridView.FirstDisplayedScrollingRowIndex = transactionsDataGridView.RowCount - 1;
        }

        public void UpdateSponsorList()
        {
            GenerateSponsors();
            this.sponsorListBox.Items.Clear();
            foreach (var Sponsor in sponsorList)
            {
                this.sponsorListBox.Items.Add(Sponsor);
            }
            this.sponsorListBox.DisplayMember = "displayMember";

            this.selectSponsorButton.Enabled = true;
        }
         

        private void selectSponsorButton_Click(object sender, EventArgs e)
        {

            if (sponsorListBox.SelectedItem != null )
            {
                if (Game.player.playerTeam.finance.sponsor.duration == 0)
                {
                    foreach (var Team in Game.teamList)
                    {
                        if (Team.name != Game.player.playerTeam.name)
                        {
                            int random = Game.random.Next(0, 5);
                            Team.finance.sponsor = sponsorList[random];
                        }
                    }

                    Game.player.playerTeam.finance.sponsor = (Sponsor)sponsorListBox.SelectedItem;
                    Game.player.playerTeam.finance.bankTotal += Game.player.playerTeam.finance.sponsor.signBonus;
                    Game.player.playerTeam.finance.transactions.Add(new FinanceTransaction(Game.date, "Sponsor Sign Bonus", Game.player.playerTeam.finance.sponsor.signBonus));
                    UpdateDataGridView();
                    sponsorList.Clear();
                    this.sponsorListBox.Items.Clear();
                    this.sponsorListBox.Items.Add(Game.player.playerTeam.finance.sponsor);

                    this.selectSponsorButton.Enabled = false;
                } 
            }

            

        }

        private void upgradeStadiumButton_Click(object sender, EventArgs e)
        {

            int upgradeCost = Game.player.playerTeam.finance.stadium.upgradeCost;

            if (Game.player.playerTeam.finance.bankTotal > upgradeCost)
            {
                if (Game.player.playerTeam.finance.stadium.level == Stadium.Level.one)
                {
                    Game.player.playerTeam.finance.bankTotal -= Game.player.playerTeam.finance.stadium.upgradeCost;
                    Game.player.playerTeam.finance.transactions.Add(new FinanceTransaction(Game.date, "Stadium Upgrade", -upgradeCost));
                    Game.player.playerTeam.finance.stadium.UpgradeStadium(Stadium.Level.two);
                    stadiumPictureBox.Image = global::SoccerSimulator5000.Properties.Resources.stadium_lvl2;
                    
                }
                else if (Game.player.playerTeam.finance.stadium.level == Stadium.Level.two)
                {
                    Game.player.playerTeam.finance.bankTotal -= Game.player.playerTeam.finance.stadium.upgradeCost;
                    Game.player.playerTeam.finance.transactions.Add(new FinanceTransaction(Game.date, "Stadium Upgrade", -upgradeCost));
                    Game.player.playerTeam.finance.stadium.UpgradeStadium(Stadium.Level.three);
                    stadiumPictureBox.Image = global::SoccerSimulator5000.Properties.Resources.stadium_lvl3;
                }
                else if (Game.player.playerTeam.finance.stadium.level == Stadium.Level.three)
                {
                    Game.player.playerTeam.finance.bankTotal -= Game.player.playerTeam.finance.stadium.upgradeCost;
                    Game.player.playerTeam.finance.transactions.Add(new FinanceTransaction(Game.date, "Stadium Upgrade", -upgradeCost));
                    Game.player.playerTeam.finance.stadium.UpgradeStadium(Stadium.Level.four);
                    stadiumPictureBox.Image = global::SoccerSimulator5000.Properties.Resources.stadium_lvl4;
                }
                else if (Game.player.playerTeam.finance.stadium.level == Stadium.Level.four)
                {
                    Game.player.playerTeam.finance.bankTotal -= Game.player.playerTeam.finance.stadium.upgradeCost;
                    Game.player.playerTeam.finance.transactions.Add(new FinanceTransaction(Game.date, "Stadium Upgrade", -upgradeCost));
                    Game.player.playerTeam.finance.stadium.UpgradeStadium(Stadium.Level.five);
                    stadiumPictureBox.Image = global::SoccerSimulator5000.Properties.Resources.stadium_lvl5;
                }
                panel1.Invalidate();
                UpdateDataGridView();
            }
        }
    }
}
